//-
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

include /app/helpers/jade/mixins
include /app/configuration/mixins

-var model = '$ctrl.clonedCluster'
-var form = 'loadBalancing'
-var loadBalancingSpi = model + '.loadBalancingSpi'
-var loadBalancingCustom = '$item.kind === "Custom"'
-var loadProbeCustom = '$item.kind === "Adaptive" && $item.Adaptive.loadProbe.kind === "Custom"'

panel-collapsible(ng-form=form on-open=`ui.loadPanel('${form}')`)
    panel-title Load balancing configuration
    panel-description
        | Load balancing component balances job distribution among cluster nodes.
        | #[a.link-success(href="https://apacheignite.readme.io/docs/load-balancing" target="_blank") More info]
    panel-content.pca-form-row(ng-if=`ui.isPanelLoaded('${form}')`)
        .pca-form-column-6
            mixin clusters-load-balancing-spi
                .ignite-form-field(ng-init='loadBalancingSpiTbl={type: "loadBalancingSpi", model: "loadBalancingSpi", focusId: "kind", ui: "load-balancing-table"}')
                    +form-field__label({ label: 'Load balancing configurations:', name: '"loadBalancingConfigurations"' })
                        +form-field__tooltip(`Load balancing component balances job distribution among cluster nodes`)

                    -let items = loadBalancingSpi
                    list-editable.pc-list-editable-with-legacy-settings-rows(
                        ng-model=items
                        name='loadBalancingConfigurations'
                    )
                        list-editable-item-edit
                            - form = '$parent.form'
                            .settings-row
                                +form-field__dropdown({
                                    label: 'Load balancing:',
                                    model: '$item.kind',
                                    name: '"loadBalancingKind"',
                                    required: true,
                                    options: '::$ctrl.Clusters.loadBalancingKinds',
                                    tip: `Provides the next best balanced node for job execution
                                    <ul>
                                        <li>Round-robin - Iterates through nodes in round-robin fashion and pick the next sequential node</li>
                                        <li>Adaptive - Adapts to overall node performance</li>
                                        <li>Random - Picks a random node for job execution</li>
                                        <li>Custom - Custom load balancing implementation</li>
                                    </ul>`
                                })(
                                    ignite-unique=`${loadBalancingSpi}`
                                    ignite-unique-property='kind'
                                )
                                    +form-field__error({ error: 'igniteUnique', message: 'Load balancing SPI of that type is already configured' })
                            .settings-row(ng-show='$item.kind === "RoundRobin"')
                                +form-field__checkbox({
                                    label: 'Per task',
                                    model: '$item.RoundRobin.perTask',
                                    name: '"loadBalancingRRPerTask"',
                                    tip: 'A new round robin order should be created for every task flag'
                                })
                            .settings-row(ng-show='$item.kind === "Adaptive"')
                                +form-field__dropdown({
                                    label: 'Load probe:',
                                    model: '$item.Adaptive.loadProbe.kind',
                                    name: '"loadBalancingAdaptiveLoadProbeKind"',
                                    placeholder: 'Default',
                                    options: '[\
                                        {value: "Job", label: "Job count"},\
                                        {value: "CPU", label: "CPU load"},\
                                        {value: "ProcessingTime", label: "Processing time"},\
                                        {value: "Custom", label: "Custom"},\
                                        {value: null, label: "Default"}\
                                    ]',
                                    tip: 'Implementation of node load probing\
                                        <ul>\
                                            <li>Job count - Based on active and waiting job count</li>\
                                            <li>CPU load - Based on CPU load</li>\
                                            <li>Processing time - Based on total job processing time</li>\
                                            <li>Custom - Custom load probing implementation</li>\
                                            <li>Default - Default load probing implementation</li>\
                                        </ul>'
                                })
                            .settings-row(ng-show='$item.kind === "Adaptive" && $item.Adaptive.loadProbe.kind')
                                .panel-details(ng-show='$item.Adaptive.loadProbe.kind === "Job"')
                                    .details-row
                                        +form-field__checkbox({
                                            label: 'Use average',
                                            model: '$item.Adaptive.loadProbe.Job.useAverage',
                                            name: '"loadBalancingAdaptiveJobUseAverage"',
                                            tip: 'Use average CPU load vs. current'
                                        })
                                .panel-details(ng-show='$item.Adaptive.loadProbe.kind === "CPU"')
                                    .details-row
                                        +form-field__checkbox({
                                            label: 'Use average',
                                            model: '$item.Adaptive.loadProbe.CPU.useAverage',
                                            name: '"loadBalancingAdaptiveCPUUseAverage"',
                                            tip: 'Use average CPU load vs. current'
                                        })
                                    .details-row
                                        +form-field__checkbox({
                                            label: 'Use processors',
                                            model: '$item.Adaptive.loadProbe.CPU.useProcessors',
                                            name: '"loadBalancingAdaptiveCPUUseProcessors"',
                                            tip: 'Divide each node\'s CPU load by the number of processors on that node'
                                        })
                                    .details-row
                                        +form-field__number({
                                            label: 'Processor coefficient:',
                                            model: '$item.Adaptive.loadProbe.CPU.processorCoefficient',
                                            name: '"loadBalancingAdaptiveCPUProcessorCoefficient"',
                                            placeholder: '1',
                                            min: '0.001',
                                            max: '1',
                                            step: '0.05',
                                            tip: 'Coefficient of every CPU'
                                        })
                                .panel-details(ng-show='$item.Adaptive.loadProbe.kind === "ProcessingTime"')
                                    .details-row
                                        +form-field__checkbox({
                                            label: 'Use average',
                                            model: '$item.Adaptive.loadProbe.ProcessingTime.useAverage',
                                            name: '"loadBalancingAdaptiveJobUseAverage"',
                                            tip: 'Use average execution time vs. current'
                                        })
                                .panel-details(ng-show=loadProbeCustom)
                                    .details-row
                                        +form-field__java-class({
                                            label: 'Load brobe implementation:',
                                            model: '$item.Adaptive.loadProbe.Custom.className',
                                            name: '"loadBalancingAdaptiveJobUseClass"',
                                            required: loadProbeCustom,
                                            tip: 'Custom load balancing SPI implementation class name.',
                                            validationActive: loadProbeCustom
                                        })

                            .settings-row(ng-show='$item.kind === "WeightedRandom"')
                                +form-field__number({
                                    label: 'Node weight:',
                                    model: '$item.WeightedRandom.nodeWeight',
                                    name: '"loadBalancingWRNodeWeight"',
                                    placeholder: '10',
                                    min: '1',
                                    tip: 'Weight of node'
                                })
                            .settings-row(ng-show='$item.kind === "WeightedRandom"')
                                +form-field__checkbox({
                                    label: 'Use weights',
                                    model: '$item.WeightedRandom.useWeights',
                                    name: '"loadBalancingWRUseWeights"',
                                    tip: 'Node weights should be checked when doing random load balancing'
                                })
                            .settings-row(ng-show=loadBalancingCustom)
                                +form-field__java-class({
                                    label: 'Load balancing SPI implementation:',
                                    model: '$item.Custom.className',
                                    name: '"loadBalancingClass"',
                                    required: loadBalancingCustom,
                                    tip: 'Custom load balancing SPI implementation class name.',
                                    validationActive: loadBalancingCustom
                                })

                        list-editable-no-items
                            list-editable-add-item-button(
                                add-item=`$ctrl.Clusters.addLoadBalancingSpi(${model})`
                                label-single='load balancing configuration'
                                label-multiple='load balancing configurations'
                            )

            +clusters-load-balancing-spi

        .pca-form-column-6
            +preview-xml-java(model, 'clusterLoadBalancing')
